package com.tutor.lzm.function.teacher.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.AdapterView;
import android.widget.ListView;

import com.tutor.lzm.AppSession;
import com.tutor.lzm.R;
import com.tutor.lzm.activity.BaseActivity;
import com.tutor.lzm.activity.TeacherDetailsAct;
import com.tutor.lzm.adapter.TeacherAdapter;
import com.tutor.lzm.bean.TeacherRecommendBean;
import com.tutor.lzm.http.BaseResponse;
import com.tutor.lzm.http.HttpClient;
import com.tutor.lzm.http.request.TeacherListRequest;
import com.tutor.lzm.http.response.TeacherListResponse;
import com.tutor.syslib.Logs;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by zzgpc on 2017-10-15.
 * 搜索老师列表
 */
public class TeacherListAct extends BaseActivity {
    private String gradeId;
    private String title;

    public static void start(Context context, String title, String gradeId) {
        Intent intent = new Intent(context, TeacherListAct.class);
        intent.putExtra("gradeId", gradeId);
        intent.putExtra("title", title);
        context.startActivity(intent);
    }

    ListView listView;
    TeacherAdapter adapter;
    private List<TeacherRecommendBean> teacherList = new ArrayList<>();


    /**
     * 加载位置坐标
     */
    private int currentPage = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_listview);
        gradeId = getIntent().getStringExtra("gradeId");
        title = getIntent().getStringExtra("title");
        initTitle(title);
        listView = (ListView) findViewById(R.id.listView);
        initListView();

        getTeacherList();
    }


    /**
     * 初始化可滑动删除litview
     */
    private void initListView() {
        adapter = new TeacherAdapter(this, teacherList);
        listView.setAdapter(adapter);
        ViewStub mViewStub = (ViewStub) findViewById(R.id.emptyView);
        listView.setEmptyView(mViewStub);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TeacherRecommendBean bean = teacherList.get(position);
                String teacherId = bean.teacherId;
                TeacherDetailsAct.start(TeacherListAct.this, teacherId);
            }
        });
    }

    /**
     * 获取老师列表
     */
    private void getTeacherList() {
        final TeacherListRequest request = new TeacherListRequest();
        request.start = currentPage;
        request.userId = AppSession.getInstance().getUserId();
        request.categoryId = gradeId;
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {

                Logs.e(tag, "response");
                try {
                    TeacherListResponse recommendList = (TeacherListResponse) response;
                    List<TeacherRecommendBean> adResponse = recommendList.teacherList;
                    teacherList.addAll(adResponse);
                    adapter.notifyDataSetChanged();
                    if (adResponse.size() == request.limit || adResponse.size() != 0) {
                        currentPage++;
                    }

                } catch (Exception e) {
                    Logs.e(tag, e.getMessage());
                }

                return;

            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                Logs.d("MainFragment", "getRecommendSubject    code" + code + "      msg=" + msg);
            }
        });

    }
}
